package com.mhkj.base.itemInfo.group.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.mhkj.base.itemInfo.group.dao.GroupDao;
import com.mhkj.base.itemInfo.group.entity.Group;
import com.mhkj.base.itemInfo.group.service.GroupService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.service.impl.BaseServiceImpl;
import com.mics.core.system.rbac.service.DictionaryService;

@Service
public class GroupServiceImpl extends BaseServiceImpl<Group> implements GroupService {
	@Resource
	private GroupDao groupDao;
	@Resource
	private DictionaryService dictionaryService;

	@Resource
	public void setGroupDao(GroupDao GroupDao) {
		super.setBaseRepository(GroupDao);
	}

	@Override
	public Pagination<Group> listByPage(Group Group, int pageIndex, int size ) {
		Specification<Group> spec = new Specification<Group>() {
			/**
			 * 序列号
			 */
			private static final long serialVersionUID = -786003485941679959L;

			@Override
			public Predicate toPredicate(Root<Group> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
				
				
//				 Join<Object, Object> tSysNation = root.join("tSysNation",JoinType.INNER);
//
//				if (!StringUtils.isEmpty(Group.getCityId())) {
//					lstPredicates.add(criteriaBuilder.equal(tSysNation.get("id"),Group.getCityId()));
//				}
//				
//				if (!StringUtils.isEmpty(Group.getName())) {
//					lstPredicates.add(criteriaBuilder.like(root.get("name").as(String.class), "%" + Group.getName() + "%"));
//				}
//				 
//				if (!StringUtils.isEmpty(Group.getAffiliatedCompanyId())) {
//					lstPredicates.add(criteriaBuilder.equal(root.get("affiliatedCompanyId").as(String.class), Group.getAffiliatedCompanyId()));
//				}
//				
			 
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		Page<Group> pageT = groupDao.findAll(spec, PageRequest.of(pageIndex, size, Sort.Direction.ASC, "id"));
		Pagination<Group> pagination = new Pagination<Group>();
		pagination.setTotal(groupDao.count(spec));
		pagination.setData(pageT.getContent());
		return pagination;
	}

	@Override
	public List<Group> getGroupByItemId(Long itemId) {
		// TODO Auto-generated method stub
		return groupDao.getGroupByItemId(itemId);
	}
 
}
